What is OpenGL ?
OpenGL is specification just like C++ standard specification
Where to download OpenGL?
There’s no code/library as it is specification. The code is being written by the GPU manufactures by suppling the drivers which contains the code. - It’s not open-source. - Cross-Platform compatibility
Different parts of openGL
Setting Up OpenGL:
- Install the necessary OpenGL libraries and drivers for your platform.
- Choose a programming language that supports OpenGL, such as C++, Python (with libraries like PyOpenGL), or Java (with libraries like LWJGL or JOGL).
Creating a Window:
- Use a windowing system library (e.g., GLFW, SDL, or FreeGLUT) to create a window where you can render OpenGL graphics.
Context and Initialization:
- Create an OpenGL context within the window to initialize the OpenGL state.
- Retrieve function pointers for the OpenGL functions using a library like GLEW or GLAD.
Rendering Loop:
- Set up a loop that will execute continuously to update and redraw the graphics.
- Within the loop, clear the frame buffer using
glClear()
to prepare for drawing new content.
Coordinate Systems:
- Understand the coordinate system used in OpenGL: the viewport, which is a 2D or 3D space where your graphics will be displayed.
- Define your objects’ positions and sizes in this coordinate system.
Creating and Binding Buffers:
- Use buffer objects (
glGenBuffers()
,glBindBuffer()
) to store vertex data, such as positions, colors, and texture coordinates. - Buffer objects are typically used with vertex arrays (
glVertexAttribPointer()
) to describe the format and layout of the vertex data.
- Use buffer objects (
Shaders and Shader Programs:
- Use shaders (vertex and fragment shaders) to define the behavior and appearance of your objects.
- Compile and link shaders into a shader program (
glCreateShader()
,glCreateProgram()
,glAttachShader()
,glLinkProgram()
) to be used during rendering.
Drawing:
- Use
glDrawArrays()
orglDrawElements()
to issue draw calls, specifying the primitive type (points, lines, triangles), vertex count, and indices (if using indices).
- Use
Transformation and Projection Matrices:
- Apply transformations (translations, rotations, scaling) to your objects using transformation matrices (
glTranslate()
,glRotate()
,glScale()
). - Use projection matrices (
gluPerspective()
,glOrtho()
) to define the perspective or orthographic projection of your scene.
- Apply transformations (translations, rotations, scaling) to your objects using transformation matrices (
Handling User Input:
- Use libraries or system functions to handle user input, such as keyboard and mouse events, to interact with your OpenGL application.
- Clean up resources: Properly release any allocated resources, such as shaders, buffers, and the OpenGL context, when your application exits.
Context/Window Toolkits
Creating window in windows in Window’s OS uses win32 api…similarly creating window is very much platform specific and uses its own api.
GLFW Library aka Window_GUI_Creator
GLFW is a lightweight library which helps us to create windows with cross compatibility by supplying - appropriate platform layout - open source in nature
OpenGL Function Extractor
GLEW/GLAD Libraries aka Extractor
GLEW/GLAD libraries is used to retrieve the openGL functions which are present in binary form in Graphics driver. The retrieving these function is platform specific. - access driver dll files - access function pointers (function definition) - provides header files (function declaration)
glew.h provides opengl function declaration .cpp file is used to extract the function definition from drivers
What is shader? A program which is running on GPU which gives output.
Cherno’s OpenGL (GLEW)
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
int main()
{
* window;
GLFWwindow
if (!glfwInit())
{
return -1;
}
= glfwCreateWindow(800, 800, "Modern OpenGL using glew", NULL, NULL);
window
// window creation failcheck
if (!window)
{
();
glfwTerminatereturn -1;
}
// Make the window's context current
(window);
glfwMakeContextCurrent
// Initializing glew (Modern OpenGL)
if (glewInit() != GLEW_OK)
::cout << "Error!" << std::endl;
std
// Printing out OpenGL Version
::cout << glGetString(GL_VERSION) << std::endl;
std
// Run loop untill window is open
while (!glfwWindowShouldClose(window))
{
(GL_COLOR_BUFFER_BIT);
glClear
// define GL triangle
(GL_TRIANGLES);
glBegin(-0.05f, 0.0f);
glVertex2f(0.0f, 0.05f);
glVertex2f(0.05f, 0.0f);
glVertex2f();
glEnd
(window);
glfwSwapBuffers
// Process poll events
();
glfwPollEvents}
();
glfwTerminatereturn 0;
}
What is Buffer? - Memory buffer or array in openGL aka memory in VRAM (GPU) - Rasterizing that vram data or buffer to draw the data on screen. This program is shader. - Shader is a program which runs on the GPU
OpenGL is state machine - it needs to select the buffer like GL_TRIANGLE - it needs to select the shader - it uses
VictorGordan_LearnOpenGL (GLAD)
- GLFW for Windows Context Creation
- GLAD to retrieve openGL function
GLFW building using CMAKE and VS. 1. CMAKE is used to build sln/workplace files which is required for the VS project 2. Using .sln file open GLFW solution and build the glfw.lib file. 3. Import/copy the library file to OpenGL project directory.
GLFW Template
#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
int main()
{
(); //Intialising GLFW (OpenGL extractor)
glfwInit
//Hinting the version
(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //Hinting the profile(CORE(modern)/COMPACTability(legacy)
glfwWindowHint
* window = glfwCreateWindow(800, 800, "VictorGordan_Opengl", NULL, NULL);
GLFWwindow
if (window == NULL) //Terminating GLFW if fails to create window
{
std::cout << "Failed to create GLFW window" << std::endl;
();
glfwTerminatereturn -1;
}
(window);
glfwMakeContextCurrent
//Closing Window only on userCloseEvent
while (!glfwWindowShouldClose(window))
{
(); //Process Poll Events
glfwPollEvents}
(window); //destroying GLFW window
glfwDestroyWindow(); //Terminating GLFW
glfwTerminatereturn 0;
}
What is Buffer?
Changing of pixel from top of the screen to bottom. New image data is know as Front Buffer whereas the screen image on which the current data is being overwritten with Front Buffer is known as Back Buffer
//Adding background with the help of GLAD and buffer
#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
int main()
{
(); //Intialising GLFW (OpenGL extractor)
glfwInit
//Hinting the version of OPENGL to glfw
(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //Hinting the profile(CORE(modern)/COMPACTability(legacy)
glfwWindowHint
* window = glfwCreateWindow(800, 800, "VictorGordan_Opengl", NULL, NULL);
GLFWwindow
if (window == NULL) //Error check if window fails to create then terminating GLFW
{
std::cout << "Failed to create GLFW window" << std::endl;
();
glfwTerminatereturn -1;
}
//introduce the window to current context
(window);
glfwMakeContextCurrent
//Load GLAD (OpenGL extractor)
();
gladLoadGL
(0, 0, 800, 800);
glViewport
(0.07f, 0.13f, 0.17f, 1.0f); //specify the color of background
glClearColor(GL_COLOR_BUFFER_BIT); //clean the back buffer and assign the new color to it
glClear(window); //swap the back buffer with the front buffer
glfwSwapBuffers
//Closing Window only on userCloseEvent
while (!glfwWindowShouldClose(window))
{
(); //Process Poll Events
glfwPollEvents}
(window); //destroying GLFW window
glfwDestroyWindow(); //Terminating GLFW
glfwTerminatereturn 0;
}
Graphics a pipeline
Graphics pipeline is made up of series of functions which takes input like vertex data and sends the output. 1. Vertex Shader 2. Shape Assembly 3. Geometry Shader